package kr.co.handysoft.smartPolice.server;

import java.net.Socket;
import java.util.List;

import kr.co.handysoft.common.registry.SmartPoliceRegistry;

import org.jdom.Element;

public class WatchingDatabaseThread extends Thread {
	
	private Element zoneInfo;
	private Socket socket;
	private String name;
	private String zone;
	private String success;
	private String fail;
	private DatabaseInformationSenderHandler handler;
	private String part;
	private List<Element> doc;

	public WatchingDatabaseThread(Element zoneInformation){

		this.zoneInfo = zoneInformation;
		this.socket = SmartPoliceRegistry.getSocket();
		
		handler = new DatabaseInformationSenderHandler(this.socket, this.zoneInfo);
		handler.setSystemId(this.zoneInfo.getAttributeValue("id"), this.zoneInfo.getParentElement().getAttributeValue("kor"));
		setup();
		
		SmartPoliceRegistry.setRunState(this.name, true);
	}

	private void setup() {
		this.name = this.zoneInfo.getAttributeValue("id");
	}

	@Override
	public void run() {
		super.run();
		while(SmartPoliceRegistry.clientRunStat){
			if (SmartPoliceRegistry.getRunState(this.name)){
				invokeWatcher();
			}
				
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void invokeWatcher() {
		this.handler.invoke(this.name, "files");
	}


}
